﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FileListMaker
{
    public interface IWildCardCondition
    {
        string ConvertCondition(string condition);
    }

    public class AsteriskWildCardCondition : IWildCardCondition
    {
        public string ConvertCondition(string condition)
        {
            int asterIndex = condition.IndexOf("*");
            if (asterIndex == -1)
                return condition;
            else if (asterIndex == 0)
                return "^.+" + condition.Substring(1) + "$";
            else
                return "^" + condition.Substring(0, asterIndex) + ".+" + condition.Substring(asterIndex+1) + "$";
        }
    }

    public class InCommaCondition : IWildCardCondition
    {
        public string ConvertCondition(string condition)
        {
            return condition.Replace(@".", @"\.");
        }
    }

    public class RegexCondition : IListCondition
    {
        private string condition;

        public RegexCondition(string condition)
        {
            this.condition = condition;
        }

        public bool IsMatch(string fileFullName)
        {
            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(condition);
            return r.IsMatch(fileFullName);
        }
    }

    public class WildCardCondition : IListCondition
    {
        private string condition;
        private List<IWildCardCondition> condConvs = new List<IWildCardCondition>();

        public WildCardCondition(string condition)
        {
            this.condition = condition;
            this.condConvs.Add(new InCommaCondition());
            this.condConvs.Add(new AsteriskWildCardCondition());
        }

        public bool IsMatch(string fileFullName)
        {
            string regPattern = condition;
            for (int i = 0; i < condConvs.Count;i++ )
                regPattern = condConvs[i].ConvertCondition(regPattern);
            RegexCondition regCheck = new RegexCondition(regPattern);
            return regCheck.IsMatch(fileFullName);
        }
    }

}
